package com.guard.location;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.guard.android.GuardAplication;

public class UserData {

    private final int MIN_ACCURACY = 51;
    private final int FRESH_LOCATION_TIME = 5;

    private LocationManager mLocationManager;
    private Location mGPSLocation = null;
    private Location mNetworkLocation = null;
    private String[] mProviders = { LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER };
    private String mUserAdrress = null;
    private String mEndPointAdrress = null;
    private boolean isManualAddress;

    public UserData() {
        mLocationManager = (LocationManager) GuardAplication.instance().getApplicationContext()
                .getSystemService(Context.LOCATION_SERVICE);
        this.init();
    }

    public Location getCurrentPosition() {
        Location location = selectBetterLocation(mNetworkLocation, mGPSLocation);
        return location;
    }

    public boolean init() {
        mGPSLocation = getLocationFromProvider(mProviders[0]);
        mNetworkLocation = getLocationFromProvider(mProviders[1]);
        return (getCurrentPosition() == null) ? false : true;
    }

    private final LocationListener mListener = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {
            if (location.getProvider().equals(mProviders[0])) {
                mGPSLocation = location;
            } else {
                mNetworkLocation = location;
            }
        }

        @Override
        public void onProviderDisabled(String provider) {
            if (provider.equals(mProviders[0])) {
                mGPSLocation = null;
            } else {
                mNetworkLocation = null;
            }
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };

    public boolean checkLocationProvidersStatus() {
        if (!isLocationAccuracyValid() && !mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            return false;
        } else {
            return true;
        }
    }

    public boolean isLocationAccuracyValid() {
        if (getCurrentPosition() != null) {
            System.out.println("Accuracy = " + getCurrentPosition().getAccuracy());
            if (getCurrentPosition().getAccuracy() < MIN_ACCURACY) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }

    }

    public String getUserAdrress() {
        return mUserAdrress;
    }

    public void setUserAdrress(String adrress) {
        this.mUserAdrress = adrress;
    }

    private Location getLocationFromProvider(String provider) {
        if (mLocationManager.isProviderEnabled(provider)) {
            Location location;
            mLocationManager.requestLocationUpdates(provider, 0, 0, mListener);
            location = mLocationManager.getLastKnownLocation(provider);
            return location;
        } else {
            return null;
        }
    }

    public boolean isManualAddress() {
        return isManualAddress;
    }

    public void setManualAddress(boolean isManualAddress) {
        this.isManualAddress = isManualAddress;
    }

    private Location selectBetterLocation(Location netLocation, Location gpsLocation) {
        if (netLocation != null || gpsLocation != null) {
            if (netLocation == null && gpsLocation != null) {
                if ((System.currentTimeMillis() - gpsLocation.getTime()) < FRESH_LOCATION_TIME * 60 * 1000) {
                    return gpsLocation;
                } else {
                    return null;
                }
            } else if (netLocation != null && gpsLocation == null) {
                if ((System.currentTimeMillis() - netLocation.getTime()) < FRESH_LOCATION_TIME * 60 * 1000) {
                    return netLocation;
                } else {
                    return null;
                }
            }

            else if (gpsLocation.getAccuracy() <= netLocation.getAccuracy()) {
                if ((System.currentTimeMillis() - gpsLocation.getTime()) < FRESH_LOCATION_TIME * 60 * 1000) {
                    return gpsLocation;
                } else {
                    if (netLocation.getAccuracy() <= MIN_ACCURACY
                            && (System.currentTimeMillis() - netLocation.getTime()) < FRESH_LOCATION_TIME * 60 * 1000) {
                        return netLocation;
                    } else {
                        return null;
                    }
                }
            } else {
                if ((System.currentTimeMillis() - netLocation.getTime()) < FRESH_LOCATION_TIME * 60 * 1000) {
                    return netLocation;
                } else {
                    if (gpsLocation.getAccuracy() <= MIN_ACCURACY
                            && (System.currentTimeMillis() - gpsLocation.getTime()) < FRESH_LOCATION_TIME * 60 * 1000) {
                        return gpsLocation;
                    } else {
                        return null;
                    }
                }

            }
        } else {
            return null;
        }
    }

    public void removeUpdates() {
        // mLocationManager.removeUpdates(mListener);
    }

    public String getEndPointAdrress() {
        return mEndPointAdrress;
    }

    public void setEndPointAdrress(String mEndPointAdrress) {
        this.mEndPointAdrress = mEndPointAdrress;
    }

}
